<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: order.php 459 2014-01-30 15:37:29Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 459 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-30 15:37:29 +0000 (Thu, 30 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */

defined('_VALID_XTC') or die('Direct Access to this location is not allowed.');

require_once (DIR_FS_CATALOG.DIR_WS_CLASSES.'class.product_konfigurator.php');
require_once(DIR_FS_INC.'inc.get_product_id.php');
require_once(DIR_FS_INC.'inc.parse_product_options.php');

class order {
    var $info, $totals, $products, $customer, $delivery, $db;

    function __construct($order_id) {
    	global $db;
		$this->info = array();
		$this->totals = array();
		$this->products = array();
		$this->customer = array();
		$this->delivery = array();
		$this->db = $db;

		$this->query($order_id);
    }

    function query($order_id) {
      	$order = $this->db->db_query("SELECT
									   *,
	                                   DATE_FORMAT(date_purchased, '%d.%m.%Y %H:%m:%i Uhr') AS purchased_date
	                                FROM
	                                 	".TABLE_ORDERS."
	                                WHERE
	                                   orders_id = ".$this->db->db_prepare($order_id));


		$totals = $this->db->db_query("SELECT 
											title, 
											text,
											class 
										FROM 
											".TABLE_ORDERS_TOTAL." 
										WHERE 
											orders_id = ".$this->db->db_prepare($order_id)." 
										ORDER BY 
											sort_order");
		while (!$totals->EOF) {
			$this->totals[] = array('title' => $totals->fields['title'],
			                        'text' => $totals->fields['text'],
			                        'class' => $totals->fields['class']);
			$totals->MoveNext();
		}

		$this->info = array('currency' => $order->fields['currency'],
		                  'currency_value' => $order->fields['currency_value'],
		                  'payment_method' => $order->fields['payment_method'],
		                  'account_type' => $order->fields['account_type'],
		                  'payment_class' => $order->fields['payment_class'],
		                  'shipping_class' => $order->fields['shipping_class'],
		                  'shipping_cost' => $order->fields['shipping_cost'],
		                  'shipping_method' => $order->fields['shipping_method'],
		                  'status' => $order->fields['customers_status'],
		                  'status_name' => $order->fields['customers_status_name'],
		                  'status_image' => $order->fields['customers_status_image'],
		                  'status_discount' => $order->fields['customers_status_discount'],
		                  'comments' => $order->fields['comments'],
		                  'language' => $order->fields['language'],
		                  'date_purchased' => $order->fields['date_purchased'],
		                  'purchased_date' => $order->fields['purchased_date'],
		                  'orders_status' => $order->fields['orders_status'],
		                  'id' => $order_id,
		                  'last_modified' => $order->fields['last_modified']);

      $this->customer = array('name' => $order->fields['customers_name'],
							  'lastname' => $order->fields['customers_lastname'],
							  'firstname' => $order->fields['customers_firstname'],
                              'company' => $order->fields['customers_company'],
                              'cID' => $order->fields['customers_cid'],
                              'vat_id' => $order->fields['customers_vat_id'],
                              'shop_id' => $order->fields['shop_id'],
                              'ID' => $order->fields['customers_id'],
                              'cIP' => $order->fields['customers_ip'],
                              'street_address' => $order->fields['customers_street_address'],
                              'suburb' => $order->fields['customers_suburb'],
                              'city' => $order->fields['customers_city'],
                              'postcode' => $order->fields['customers_postcode'],
                              'state' => $order->fields['customers_state'],
                              'country' => $order->fields['customers_country'],
                              'format_id' => $order->fields['customers_address_format_id'],
                              'telephone' => $order->fields['customers_telephone'],
                              'email_address' => $order->fields['customers_email_address']);

      $this->delivery = array(	'name' => $order->fields['delivery_name'],
								'lastname' => $order->fields['delivery_lastname'],
								'firstname' => $order->fields['delivery_firstname'],
								'company' => $order->fields['delivery_company'],
								'street_address' => $order->fields['delivery_street_address'],
								'suburb' => $order->fields['delivery_suburb'],
								'city' => $order->fields['delivery_city'],
								'postcode' => $order->fields['delivery_postcode'],
								'state' => $order->fields['delivery_state'],
								'country' => $order->fields['delivery_country'],
								'format_id' => $order->fields['delivery_address_format_id']);

      $this->billing = array(	'name' => $order->fields['billing_name'],
								'lastname' => $order->fields['billing_lastname'],
								'firstname' => $order->fields['billing_firstname'],
								'company' => $order->fields['billing_company'],
								'street_address' => $order->fields['billing_street_address'],
								'suburb' => $order->fields['billing_suburb'],
								'city' => $order->fields['billing_city'],
								'postcode' => $order->fields['billing_postcode'],
								'state' => $order->fields['billing_state'],
								'country' => $order->fields['billing_country'],
								'format_id' => $order->fields['billing_address_format_id']);

	      $index = 0;
	      $orders_products = $this->db->db_query("
					SELECT
						op.*, p.products_status 
					FROM 
						".TABLE_ORDERS_PRODUCTS." op
						inner join ".TABLE_PRODUCTS." p on (op.products_id = p.products_id)
					WHERE 
						op.orders_id = ".$this->db->db_prepare($order_id));

		while(!$orders_products->EOF) {
			$options_description = '';
			$products_name_suffix = ($orders_products->fields['products_status'] == 2 ? ' (Hausmodell)' : '');

			if ($orders_products->fields['products_id']!=get_product_id($orders_products->fields['products_id'])) {
				$products_name_suffix = ' (konfiguriert)';
		    if ( ($konfigurator = $this->use_konfigurator(true, get_product_id($orders_products->fields['products_id']))) !== false ) {
					$options_description = $konfigurator->get_description(parse_product_options($orders_products->fields['products_id']));
				}
      }

			$this->products[$index] = array(
				'qty' => $orders_products->fields['products_quantity'],
				'name' => $orders_products->fields['products_name'] . $products_name_suffix,
				'image' => $orders_products->fields['products_image'],
				'id' => $orders_products->fields['products_id'],
				'opid' => $orders_products->fields['orders_products_id'],
				'options_description' => $options_description,
				'model' => $orders_products->fields['products_model'],
				'tax' => $orders_products->fields['products_tax'],
				'price' => $orders_products->fields['products_price'],
				'shipping_time' => $orders_products->fields['products_shipping_time'],
				'discount' => $orders_products->fields['products_discount_made'],
				'final_price' => $orders_products->fields['final_price'],
				'allow_tax' => $orders_products->fields['allow_tax']);
			
			$subindex = 0;
			$attributes = $this->db->db_query("SELECT 
													products_options, 
													products_options_values, 
													options_values_price, 
													price_prefix 
												FROM 
													".TABLE_ORDERS_PRODUCTS_ATTRIBUTES." 
												WHERE 
													orders_id = ".$this->db->db_prepare($order_id)." 
												AND 
													orders_products_id = '".$orders_products->fields['orders_products_id']."'");
			if ($attributes->_numOfRows > 0) {
				while (!$attributes->EOF) {
					$this->products[$index]['attributes'][$subindex] = array('option' => $attributes->fields['products_options'],
					                                                         'value' => $attributes->fields['products_options_values'],
					                                                         'prefix' => $attributes->fields['price_prefix'],
					                                                         'price' => $attributes->fields['options_values_price']);
					
					$subindex++;
					$attributes->MoveNext();
				}
			}
			$index++;
			$orders_products->MoveNext();
		}
	}
	

	public function use_konfigurator($from_logik, $products_id) {
    if ($from_logik && KONFIGURATOR_ENABLE == 'true') {
    	$konfigurator = new product_konfigurator($products_id);
    	return $konfigurator;
    }
    else
    	return false;
	}
	
}